package com.newdjk.newdoctor.base;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.newdjk.newdoctor.MyApplication;
import com.newdjk.newdoctor.R;
import com.newdjk.newdoctor.adapter.MinFunctionAdapter;
import com.newdjk.newdoctor.entity.AppLicationEntity;
import com.newdjk.newdoctor.fragment.HomeFragment;
import com.newdjk.newdoctor.utils.AppLicationUtils;
import com.newdjk.newdoctor.view.GridItemDivider;
import com.newdjk.newdoctor.view.TabClickListener;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseHomeView extends RelativeLayout {
    private static final String TAG = "BaseHomeView";
    private RecyclerView rectleview;
    private TextView mTitle;
    private List<AppLicationEntity> listall = new ArrayList<>();
    private MinFunctionAdapter mFunctionAdapter;
    private TabClickListener onTabClickListener;
    private LinearLayout container;

    public BaseHomeView(Context context) {
        super(context);
        init();
    }

    public BaseHomeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BaseHomeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    /**
     * 初始化相关UI元素
     */
    private void init() {
        inflate(getContext(), R.layout.recyleview, this);
        mTitle = findViewById(R.id.title);
        rectleview = findViewById(R.id.rectleview);
        container = findViewById(R.id.action_bar_container);
        mTitle.setText(setTitle());
        GridLayoutManager mManagerLayout = new GridLayoutManager(MyApplication.getContext(), 3);
        rectleview.setLayoutManager(mManagerLayout);
        mFunctionAdapter = new MinFunctionAdapter(listall);
        rectleview.setAdapter(mFunctionAdapter);
        GridItemDivider gridLayoutDivider = new GridItemDivider(2, Color.parseColor("#E6E6E6"));
        rectleview.addItemDecoration(gridLayoutDivider);
        mFunctionAdapter.setNewData(listall);
        if (mFunctionAdapter != null) {
            mFunctionAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    if (onTabClickListener != null) {
                        onTabClickListener.onSessionClick(listall.get(position));
                    }
                }
            });
        }
    }

    public abstract String setTitle();

    public void setTabClick(TabClickListener listener) {
        onTabClickListener = listener;
    }

    public void setData(List<AppLicationEntity> list) {
        listall.clear();
        listall.addAll(list);
        mFunctionAdapter.setNewData(listall);
        if (listall.size()==0){
            container.setVisibility(GONE);
        }else {
            container.setVisibility(VISIBLE);
        }
    }

    public void setRedTitle(String tabtitle, int red) {
        Log.d(TAG, tabtitle + "红点" + red);

        for (int i = 0; i < listall.size(); i++) {
            if (listall.get(i).getAppDesc().equals(tabtitle)){
                listall.get(i).setUnReadNum(red);
            }
        }
        if (listall.size()==0){
            container.setVisibility(GONE);
        }else {
            container.setVisibility(VISIBLE);
        }
        mFunctionAdapter.setNewData(listall);
        mFunctionAdapter.notifyDataSetChanged();

    }
}

